<?php

namespace App\Listeners\Plane\PlaneOrder;

use App\Events\Plane\PlaneOrder\PlaneOrderCreatedEvent;
use App\Notifications\Plane\PlaneOrder\WaitingPayNotify;
use App\Trip\Orders\UserLastOrder;
use Carbon\Carbon;

class PlaneOrderCreatedListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param object $event
     * @return void
     */
    public function handle(PlaneOrderCreatedEvent $event)
    {
        $order = $event->order;
        // 1 自动取消订单
        $order->autoCancel();
        // 2 订单自动取消前 10分钟
        if ($order->getAutoCancelMinutes() > 10) {
            // 0 订单创建通知
            $delay = Carbon::parse($order->getWillCancelAt())->subMinutes(10);
            $order->notify((new WaitingPayNotify($order))->delay($delay));
        } else {
            // 0 订单创建通知
            $order->notify((new WaitingPayNotify($order)));
        }
        UserLastOrder::setLastOrder($order);
    }
}
